package com.service.component.role.controller;

import com.service.component.common.CommonResult;
import com.service.component.common.enumerate.OperateTypeEnum;
import com.service.component.log.aspect.SysOperateLog;
import com.service.component.relation.entity.GroupRoleRelation;
import com.service.component.relation.entity.UserRoleRelation;
import com.service.component.relation.services.RelationServices;
import com.service.component.role.entity.Role;
import com.service.component.role.services.RoleServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：Calvin
 * @create ：2021/7/22 13:16
 * @description：角色管理控制
 * @modified By：
 * @version: 1.0.0$
 */
@Slf4j
@RestController
@Api(value = "角色管理")
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RelationServices relationServices;

    @Autowired
    private RoleServices roleServices;

    @PostMapping("insertRole")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    @SysOperateLog(descrption = "新增角色",operateType = OperateTypeEnum.ADD)
    public CommonResult insertPermission(@RequestBody Role role){
        Role roleByName = roleServices.getRoleByPermName(role.getRoleNameEN());
        if(ObjectUtils.isEmpty(roleByName)){
            return roleServices.insertRole(role);
        }
        return CommonResult.commonResult(CommonResult.ResultCode.DATA_EXIST);
    }

    @PostMapping("giveUserRole")
    @ApiOperation(value = "赋予用户角色", notes = "赋予用户角色")
    @SysOperateLog(descrption = "赋予用户角色",operateType = OperateTypeEnum.ADD)
    public CommonResult giveUserRole(@RequestBody UserRoleRelation userRoleRelation){
         return relationServices.insertOrUpdateRelation(userRoleRelation);
    }

    @PostMapping("giveGroupRole")
    @ApiOperation(value = "赋予组角色", notes = "赋予组角色")
    @SysOperateLog(descrption = "赋予组角色",operateType = OperateTypeEnum.ADD)
    public CommonResult giveGroupRole(@RequestBody GroupRoleRelation groupRoleRelation){
        return relationServices.insertOrUpdateRelation(groupRoleRelation);
    }
}
